home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / amiga-magazin-pd / 03-95-2 / datatypes / lib.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  6KB  |  194 lines

  1. #include <intuition/intuitionbase.h>
  2.  
  3. #include <datatypes/pictureclass.h>
  4.  
  5. #include <graphics/gfxbase.h>
  6. #include <graphics/gfxmacros.h>
  7. #include <graphics/modeid.h>
  8.  
  9. #include <dos/dosextens.h>
  10. #include <dos/rdargs.h>
  11.  
  12. #include <exec/memory.h>
  13.  
  14. #include <clib/intuition_protos.h>
  15. #include <clib/graphics_protos.h>
  16. #include <clib/utility_protos.h>
  17. #include <clib/layers_protos.h>
  18. #include <clib/exec_protos.h>
  19. #include <clib/dos_protos.h>
  20. #include <clib/alib_protos.h>
  21.  
  22. #include <string.h>
  23.  
  24. struct Library * __asm __saveds LibInit(register __a0 BPTR Segment,
  25.                                 register __d0 struct Library *Base);
  26. struct Library * __asm __saveds LibOpen(register __a6 struct Library *Base);
  27. BPTR __asm __saveds             LibExpunge(register __a6 struct Library *Base);
  28. BPTR __asm __saveds             LibClose(register __a6 struct Library *Base);
  29. LONG __asm __saveds             LibNull(register __a6 struct Library *Base);
  30.  
  31. Class * __saveds         GetClassEngine(VOID);
  32.  
  33. BPTR                     LibSegment  = NULL;
  34. UBYTE __aligned          LibName[]   = "geo.datatype";
  35. UBYTE __aligned          LibID[]     = "geo.datatype 1.0 (11.7.94)\r\n";
  36.  
  37. struct ExecBase         *SysBase;
  38. struct DosLibrary       *DOSBase;
  39. struct IntuitionBase    *IntuitionBase;
  40. struct GfxBase          *GfxBase;
  41. struct Library          *LayersBase,
  42.                         *UtilityBase,
  43.                         *DataTypesBase,
  44.                         *SuperClassBase;
  45. struct SignalSemaphore   LockSemaphore;
  46. Class                   *ImageClass;
  47.  
  48. APTR LibVectors[] =  /* die Sprungtabelle */
  49. {   LibOpen,
  50.     LibClose,
  51.     LibExpunge,
  52.     LibNull,
  53.     GetClassEngine,
  54.     (APTR)-1
  55. };
  56.  
  57. struct { ULONG DataSize; APTR Table; APTR Data; struct Library * (*Init)(); }
  58.     __aligned LibInitTab =
  59. {
  60.     sizeof(struct Library),
  61.     LibVectors,
  62.     NULL,
  63.     LibInit
  64. };
  65.  
  66. struct Library * __asm __saveds
  67. LibInit(register __a0 BPTR Segment,register __d0 struct Library *Base)
  68. {
  69.     Base -> lib_Node . ln_Type  = NT_LIBRARY;
  70.     Base -> lib_Node . ln_Name  = LibName;
  71.     Base -> lib_Flags           = LIBF_CHANGED | LIBF_SUMUSED;
  72.     Base -> lib_Version         = 1;
  73.     Base -> lib_Revision        = 0;
  74.     Base -> lib_IdString        = LibID;
  75.  
  76.     LibSegment = Segment;
  77.     SysBase = *(struct ExecBase **)4;
  78.     InitSemaphore(&LockSemaphore);
  79.     return(Base);
  80. }
  81.  
  82. struct Library * __asm __saveds
  83. LibOpen(register __a6 struct Library *Base)
  84. {
  85.     Base -> lib_OpenCnt++;
  86.     Base -> lib_Flags &= ~LIBF_DELEXP;
  87.  
  88.     if(Base -> lib_OpenCnt == 1)
  89.     {   ObtainSemaphore(&LockSemaphore);
  90.         if(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library",37))
  91.         {   if(IntuitionBase =
  92.                 (struct IntuitionBase *)OpenLibrary("intuition.library",37))
  93.             {   if(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",39))
  94.                 {   if(UtilityBase = OpenLibrary("utility.library",0))
  95.                     {   if(LayersBase = OpenLibrary("layers.library",0))
  96.                         {   if(DataTypesBase = OpenLibrary("datatypes.library",0))
  97.                             {   if(SuperClassBase =
  98.                                     OpenLibrary("datatypes/picture.datatype",0))
  99.                                 {   if(ImageClass = MakeClass("geo.datatype",
  100.                                         PICTUREDTCLASS,NULL,NULL,NULL))
  101.                                     {
  102.                                         extern ULONG ClassDispatch();
  103.                                         ImageClass -> cl_Dispatcher . h_Entry =
  104.                                             (HOOKFUNC)ClassDispatch;
  105.                                         AddClass(ImageClass);
  106.                                         ReleaseSemaphore(&LockSemaphore);
  107.                                         return(Base);
  108.                                     }
  109.                                     CloseLibrary(SuperClassBase);
  110.                                     SuperClassBase = NULL;
  111.                                 }
  112.                                 CloseLibrary(DataTypesBase);
  113.                                 DataTypesBase = NULL;
  114.                             }
  115.                             CloseLibrary(LayersBase);
  116.                             LayersBase = NULL;
  117.                         }
  118.                         CloseLibrary(UtilityBase);
  119.                         UtilityBase = NULL;
  120.                     }
  121.                     CloseLibrary(GfxBase);
  122.                     GfxBase = NULL;
  123.                 }
  124.                 CloseLibrary(IntuitionBase);
  125.                 IntuitionBase = NULL;
  126.             }
  127.             CloseLibrary(DOSBase);
  128.             DOSBase = NULL;
  129.         }
  130.         ReleaseSemaphore(&LockSemaphore);
  131.         return(NULL);
  132.     }
  133.     else
  134.         return(Base);
  135. }
  136.  
  137. BPTR __asm __saveds
  138. LibExpunge(register __a6 struct Library *Base)
  139. {
  140.     BPTR TempSegment = LibSegment;
  141.  
  142.     if(!Base -> lib_OpenCnt && TempSegment)
  143.     {   Remove((struct Node *)Base);
  144.         FreeMem((BYTE *)Base - Base -> lib_NegSize,
  145.             Base -> lib_NegSize + Base -> lib_PosSize);
  146.         LibSegment = NULL;
  147.         return(TempSegment);
  148.     }
  149.     else
  150.     {   Base -> lib_Flags |= ~LIBF_DELEXP;
  151.         return(NULL);
  152.     }
  153. }
  154.  
  155. BPTR __asm __saveds
  156. LibClose(register __a6 struct Library *Base)
  157. {
  158.     if(!(Base -> lib_OpenCnt--)) Base -> lib_OpenCnt = 0;
  159.     if(!Base -> lib_OpenCnt)
  160.     {
  161.         ObtainSemaphore(&LockSemaphore);
  162.         if(ImageClass)
  163.         {   RemoveClass(ImageClass);
  164.             FreeClass(ImageClass);
  165.             ImageClass = NULL;
  166.         }
  167.         CloseLibrary(SuperClassBase); SuperClassBase = NULL;
  168.         CloseLibrary(DataTypesBase);  DataTypesBase = NULL;
  169.         CloseLibrary(LayersBase);     LayersBase = NULL;
  170.         CloseLibrary(UtilityBase);    UtilityBase = NULL;
  171.         CloseLibrary(IntuitionBase);  IntuitionBase = NULL;
  172.         CloseLibrary(GfxBase);        GfxBase = NULL;
  173.         CloseLibrary(DOSBase);        DOSBase = NULL;
  174.         ReleaseSemaphore(&LockSemaphore);
  175.     }
  176.     if((Base -> lib_Flags & LIBF_DELEXP) && !Base -> lib_OpenCnt)
  177.         return(LibExpunge(Base));
  178.     return(NULL);
  179. }
  180.  
  181. LONG __asm __saveds
  182. LibNull(register __a6 struct Library *Base)
  183. {
  184.     return(NULL);
  185. }
  186.  
  187. Class * __saveds GetClassEngine()
  188. {   Class *class;
  189.     ObtainSemaphore(&LockSemaphore);
  190.     class = ImageClass;
  191.     ReleaseSemaphore(&LockSemaphore);
  192.     return(class);
  193. }
  194.